package mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import modelo.Casilla;
import modelo.Comodin;
import modelo.Fruta;

public class AdmPersistenciaCasilla  extends AdmPersistencia<Casilla> {

	private static AdmPersistenciaCasilla instancia;
	
	private AdmPersistenciaCasilla()
	{
		
	}
	
	public static AdmPersistenciaCasilla getInstancia()
	{
		if (instancia == null)
			instancia = new AdmPersistenciaCasilla();
		return instancia;
	}

	@Override
	public void insertar(Casilla a) {
		// TODO Auto-generated method stub
		try
		{
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("insert into dbo.Casilla values (?,?,?)");
			//agregar campos
			s.setInt(1,(int) a.getId());
			s.setInt(2, a.getIdTragamonedas());
			s.setString(3,a.getName());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
	}

	@Override
	public void actualizar(Casilla a) {
		// TODO Auto-generated method stub
				try
				{
					Connection con = PoolConnection.getPoolConnection().getConnection();
					PreparedStatement s = con.prepareStatement("update dbo.Casilla " +
							"set id_casilla = ?," +
							"id_tragamonedas =?," +
							"nombre =? where id_casilla = ?");
			s.setFloat(2, a.getIdTragamonedas());
			s.setString(3,a.getName());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
	}

	@Override
	public void borrar(Casilla d) {
		// TODO Auto-generated method stub
		try
		{
			Casilla a = (Casilla)d;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("delete from dbo.Casilla where id_tragamonedas = ?");
			s.setInt(1, (int) a.getId());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
		
	}

	@Override
	public ArrayList<Casilla> seleccionar(long id) {
		// TODO Auto-generated method stub
				ArrayList<Casilla> casillas;
				
						try
						{
							casillas = new ArrayList<Casilla>();
							Casilla a = null;
							Connection con = PoolConnection.getPoolConnection().getConnection();
							PreparedStatement s = con.prepareStatement("select id_casilla, id_tragamonedas, nombre  from dbo.Casilla where id_tragamonedas = ?");
							s.setLong(1,id);
							ResultSet result = s.executeQuery();
							while (result.next())
							{
								long idCasilla =  result.getLong(1);
								int idTragamonedas = (int) id;
								String nombre = result.getString(2);
								if (nombre == "Comodin"){
									a = new Comodin(idCasilla, idTragamonedas, null);
								}
								else{
									a = new Fruta(idCasilla, idTragamonedas, nombre);	
								}
								
								casillas.add(a);
							}
							
							PoolConnection.getPoolConnection().realeaseConnection(con);
							return casillas;
						}
						catch (Exception e)
						{
							System.out.println(e.getMessage());
						}
				return null;
	}

	@Override
	public Casilla buscar(long id) {
		// TODO Auto-generated method stub
				try
				{
					Casilla a = null;
					Connection con = PoolConnection.getPoolConnection().getConnection();
					PreparedStatement s = con.prepareStatement("select id_casilla, id_tragamonedas, nombre from dbo.Casilla where id_casilla = ?");
					s.setLong(1,id);
					ResultSet result = s.executeQuery();
					while (result.next())
					{
						long idCasilla = result.getLong(1);
						int idTragamonedas = result.getInt(2);
						String strName = result.getString(3);
						
						if(strName == "Comodin"){
							a = new Comodin(idCasilla, idTragamonedas,  null);
						} else {
							a = new Fruta(idCasilla, idTragamonedas, strName);
						}

					}					
					PoolConnection.getPoolConnection().realeaseConnection(con);
					return a;
				}
				catch (Exception e)
				{
					System.out.println(e.getMessage());
				}
				return null;
	}

}
